import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import ArticleList from '@/views/ArticleList.vue'
import ArticleEdit from '@/views/ArticleEdit.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import ArticleView from '@/views/ArticleView.vue'
// 你可以根据需要新建这些页面文件
const ClassManage = () => import('@/views/ClassManage.vue')
const DraftManage = () => import('@/views/DraftManage.vue')
const DepartmentManage = () => import('@/views/DepartmentManage.vue')
const EmployeeManage = () => import('@/views/EmployeeManage.vue')
// const ArticleView = () => import('@/views/ArticleView.vue')

const routes = [
  { path: '/home', component: HomeView },
  { path: '/class', component: ClassManage },
  { path: '/draft', component: DraftManage },
  { path: '/department', component: DepartmentManage },
  { path: '/employee', component: EmployeeManage },
  { path: '/articles', component: ArticleList },
  { path: '/articles/edit', component: ArticleEdit },
  { path: '/article/view', component: ArticleView },
  { path: '/', redirect: '/home' },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (!token && to.path !== '/login' && to.path !== '/register') {
    next('/login')
  } else {
    next()
  }
})

export default router
